package clientGUI.userPanels.Controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import college.Request;
import college.RequestTreatment;

import clientGUI.Controllers.Controller;
import clientserver.AttachedFile;
import clientserver.Client;
import clientserver.Packet;
import clientserver.QueryResult;

public class RequestController extends  Controller {

	
 
	 private Request request;
	 private RequestTreatment requestt;
	private String Requestinitiator;
	private ArrayList<String> attachedfiles=null; 
	private int rid;

	//getters and setters/////////////////////////////
	public int getRid() {
		return rid;
	}

	public void setRid(int r) {
		rid = r;

	
	}
	
public RequestController(Client c)
{
	client=c;
	attachedfiles= new ArrayList<String>();
}

	public String getRequestinitiator() {
		return Requestinitiator;
	}

	public void setRequestinitiator(String requestinitiator) {
		Requestinitiator = requestinitiator;
	}
	
	public void setClient(Client cl)
	{
		 client=cl;
		
	}
	
	///////////////////////////////////////////////////
	
	
	public void addfile( String dir,String fn)
	{
	

		
		AttachedFile f =new AttachedFile(false);
		
	
				f.setonclient(dir, fn);


			
				try {
					f.loadfile();
					
					
					
				} catch (IOException e) {
					
					e.printStackTrace();
				}
			
			f.setRid(rid);
			try {
				try {
					client.SendFile(f);
						}
				
				catch (InterruptedException e) {
				
					e.printStackTrace();
				}
			}
			
			
			catch (IOException e) {
			
				e.printStackTrace();
			}

		attachedfiles.add(fn);	
			
	
	}
	
	public String getAttachedFiles()
	{
		ArrayList<String> result=new ArrayList<String>();
		if(attachedfiles.size()==0)
		{
			System.out.println("No file attached");
			result.add("No Files attached");
		}
		else
		{
			result=attachedfiles;
		}
		

		String answ="";
		Iterator<String> iter=result.iterator();
		while(iter.hasNext())
		{
			answ=answ+"\n"+iter.next();
		}
		return answ;
		
	}
	
	 public void  setRequest(Request r)
	 {
		 request=r;
		 request.setId(rid);
		 request.setInitiator(Requestinitiator);
				 request.setClient(client);
		 requestt =new RequestTreatment(request);
			requestt.setClient(client);
		 
	 }
	 
	 ///////////////////constructor/////////////////////////
	public  RequestController()
	{
		attachedfiles = new ArrayList<String>();
		request=null;
		requestt=null;
		}
	
	
	//save  request and return his id number ( which automatically generated by MySql)
	public int SaveRequest()
	{
	request.addToDb();
 return lastrequestid();
	}
	
	public void saveRequestDetails()
	{
	requestt.savecuruntstatetoDB();
	}
	
	public int  lastrequestid()
	{
		QueryResult t;
		 Packet p=new Packet("SELECT max(id)  as res  FROM requests");
		  t=(QueryResult) client.SendPacketAndWait(p);
		  
		 String res=t.getString("res");
		 if(res.equals("null")) return 0;
		 
		 int r= Integer.parseInt(res);
			
 return r;

	}


	
	
	
	
	

}
